﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Net.Sockets;

/*******************************************************************************
 * 工作模式示例调用：
 * 
 *  构造
 *  MotorSwitch __ms = new MotorSwitch();
 *  连接
 *  __ms.Connect("127.0.0.1", 4001)或者 __ms.Connect(COM1");
 *  断开
 *  __ms.Disconnect();
 *  
 *  归零操作： bool result = __ms.SW_Work_Zero_Write();
 *            if(!result)GetErrInfo();----->可选
 *            
 *  开关操作： bool result = __ms.SW_Work_Switch_Write(1~6,true);
 *            if(!result)GetErrInfo();----->可选
 * 
 * ****************************************************************************/

namespace JcMotorSwitch
{
    public partial class Form1 : Form
    {
        SerialPort __serialPort;
        TcpClient __tcpClient;
        bool __testRunning;
        MotorSwitch __ms;

        public Form1()
        {
            InitializeComponent();

            __ms = new MotorSwitch();
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;

            if (btn.Text.Equals("连接"))
            {
                if (__ms.Connect(this.comboBox1.Text)) btn.Text = "断开";
            }
            else
            {
                btn.Text = "连接";
                __ms.Disconnect();
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Button btn = sender as Button;

            if (btn.Text.Equals("连接"))
            {               
                try
                {
                    if (__ms.Connect(this.radioButton1.Checked ? 0 : 1, this.tbIP.Text)) btn.Text = "断开";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                btn.Text = "连接";
                __ms.Disconnect();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            btn.Enabled = false;

            this.__ms.SW_Work_Zero_Write();

            btn.Enabled = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Byte[] data = new Byte[4];
            ushort value= (ushort)this.numericUpDown1.Value;
            this.__ms.SW_Fact_X_SpeedWork_Write(value);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Byte[] data = new Byte[4];
            ushort value = (ushort)this.numericUpDown2.Value;
            this.__ms.SW_Fact_X_SpeedUser_Write(value);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Byte[] data = new Byte[4];
            ushort value = (ushort)this.numericUpDown3.Value;
            this.__ms.SW_Fact_X_SpeedReturn_Write(value);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Byte[] data = new Byte[4];
            ushort value = (ushort)this.numericUpDown4.Value;
            this.__ms.SW_Fact_X_SpeedLow_Write(value);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Byte[] data = new Byte[4];
            Byte value = (Byte)(this.numericUpDown5.Value*100);
            this.__ms.SW_Fact_X_Space_Write(value);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            ushort value = 0;
            this.__ms.SW_Fact_X_SpeedWork_Read(ref value);
            this.numericUpDown1.Value = (decimal)value;
        }

        private void button12_Click(object sender, EventArgs e)
        {
            ushort value = 0;
            this.__ms.SW_Fact_X_SpeedUser_Read(ref value);
            this.numericUpDown2.Value = (decimal)value;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            ushort value = 0;
            this.__ms.SW_Fact_X_SpeedReturn_Read(ref value);
            this.numericUpDown3.Value = (decimal)value;
        }

        private void button14_Click(object sender, EventArgs e)
        {
            ushort value = 0;
            this.__ms.SW_Fact_X_SpeedLow_Read(ref value);
            this.numericUpDown4.Value = (decimal)value;
        }

        private void button15_Click(object sender, EventArgs e)
        {
            Byte value = 0;
            this.__ms.SW_Fact_X_Space_Read(ref value);
            this.numericUpDown5.Value = (decimal)(value * 0.01f);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            Byte[] data = new Byte[4];
            ushort value = (ushort)this.numDripStep.Value;

            if (this.cbIsCur.Checked) value = 0xFFFF;

            this.__ms.SW_Fact_Config_Write((Byte)this.numDripPort.Value, value);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            Byte[] data = new Byte[4];
            ushort value = (ushort)this.numDripMan.Value;

            this.__ms.SW_Fact_User_Write(!this.cbDir.Checked, this.cbNeed3.Checked, value);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;

            btn.Enabled = false;

            Byte[] data = new Byte[4];

            Byte port = (Byte)(this.cbPort.SelectedIndex + 1);

            if (!this.__ms.SW_Work_Switch_Write(port, this.cbNeed2.Checked))
            {
                MessageBox.Show("失败！");
            }

            btn.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = sender as CheckBox;

            this.numDripStep.Enabled = !cb.Checked;
        }

        private void button18_Click(object sender, EventArgs e)
        {
            ushort position = 0;
            Byte port = (Byte)(this.numDripPort.Value);

            this.__ms.SW_Fact_Config_Read((Byte)this.numDripPort.Value, ref position);

            this.numDripStep.Value = position;
        }        

        private void button17_Click_1(object sender, EventArgs e)
        {
            Button btn = sender as Button;

            if (btn.Text.Equals("运行"))
            {
                btn.Text = "停止";
                this.__testRunning = true;
                new Thread(new ThreadStart(delegate()
                {
                    this.__ms.SW_Work_Zero_Write();

                    for (int i = 0; i < (int)this.numTest.Value; i++)
                    {                        if (!this.__testRunning) break;

                        bool resWrite = this.__ms.SW_Work_Switch_Write((Byte)((i % 6) + 1), this.cbTestNeed.Checked);

                        if (!resWrite)
                        {
                            MessageBox.Show("Need Operation Failed!");
                            break;
                        }

                        Thread.Sleep(1000);

                        this.Invoke(new MethodInvoker(delegate {
                            this.listBox1.Items.Add(string.Format("==>Cnt:{0},Port:{1},Result={2}", i + 1, (Byte)((i % 6) + 1),resWrite));
                            this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
                        }));

                        if ((i % 6) == 5)
                        {
                            this.__ms.SW_Work_Zero_Write();
                            Thread.Sleep(1000);
                        }
                    }

                    this.Invoke(new MethodInvoker(delegate
                    {
                        this.btnTest.Text = "运行";
                    }));
                })).Start();
            }
            else
            {
                btn.Text = "运行";
                lock (this) this.__testRunning = false;
            }
        }

        private void comboBox1_Click(object sender, EventArgs e)
        {
            this.comboBox1.Items.Clear();
            this.comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
        }
    }
}
